package src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine;

import java.util.HashSet;

import javax.swing.JPanel;

import src.edu.wayne.cs.csc4110.hw6.interfaces.FrameWork;
import src.edu.wayne.cs.csc4110.hw6.interfaces.State;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;

public class StateMachine implements State {
	private State loggedInState;
	private State registerState;
	private State privateChatState;
	private State notLoggedInState;
	private State changeToNextUserState;
	private FrameWork frameWork;
	private User currentUser;

	private State state;
	
	public StateMachine(FrameWork frameWorkImpl) {

		this.frameWork = frameWorkImpl;
		loggedInState = new LoggedInState(this);
		registerState = new RegisterState(this);
		privateChatState = new PrivateChatState(this);
		notLoggedInState = new NotLoggedInState(this);
		changeToNextUserState = new ChangeToNextUserState(this);
		this.state = notLoggedInState;
		setCurrentUser(null);
	}
	
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
		if (! frameWork.isUserInCurrentSession(currentUser))
		{
			frameWork.addUserToSession(currentUser);
		}
	}

	public void setState(State state)
	{
		this.state = state;
	}

	@Override
	public void logOutCurrentUser(){
		state.logOutCurrentUser();
	}
	
	@Override
	public void goToLoggedInState() {
		state.goToLoggedInState();
	}
	
	@Override
	public void goToRegisterState() {
		state.goToRegisterState(); 
	}

	@Override
	public void goToPrivateChatState() {
		state.goToPrivateChatState();
	}

	@Override
	public void goToNotLoggedInState() {
		state.goToNotLoggedInState();		
	}
	
	public FrameWork getFrameWork(){
		return this.frameWork;
	}

	public void setDisplayPanel(JPanel newPanel, String newTitle){
		this.frameWork.setCurrentPanel(newPanel, newTitle);
	}

	@Override
	public void goToChangeToNextUserState() {
		state.goToChangeToNextUserState();	
	}

	@Override
	public User getCurrentUser() {
		return this.currentUser;
	}

	/**
	 * Delegates this task to the framework.
	 * @param userName
	 * @return
	 */
	public User getUser(String userName){
		return frameWork.getDataBase().getUser(userName);
	}
	
	/**
	 * Delegates this task to the framework.
	 * @return HashSet<User>
	 */
	public HashSet<User> getUsersInSession(){
		return getFrameWork().getUsersInSession();
	}
	
	/**
	 * Delegates this task to the framework.
	 * @param email
	 * @param pwd
	 * @return
	 */
	public boolean login(String email, String pwd){
		return frameWork.login(email, pwd);
	}
		
	public State getLoggedInState(){return loggedInState;}
	public State getRegisterState(){return registerState;}
	public State getPrivateChatState(){return privateChatState;}
	public State getNotLoggedInState(){return notLoggedInState;}
	public State getChangeToNextUserState(){return changeToNextUserState;}
	
}
